package com.thunder.soft.ui.adapter;

import android.content.Intent;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;

import com.thunder.soft.R;
import com.thunder.soft.bean.HomeArticleBean;
import com.thunder.soft.common.base.BaseAdapter;
import com.thunder.soft.common.base.BaseViewHolder;
import com.thunder.soft.databinding.HomeArticleItemBinding;
import com.thunder.soft.ui.activity.DetailsActivity;
import com.thunder.soft.vm.HomeViewModel;

import java.util.List;

public class HomeArticleAdapter extends BaseAdapter<HomeArticleBean, HomeArticleItemBinding> {

    private static final String TAG = HomeArticleAdapter.class.getName();
    private HomeViewModel homeViewModel;

    public void setHomeViewModel(HomeViewModel homeViewModel) {
        this.homeViewModel = homeViewModel;
    }

    @Override
    protected int layoutId(int viewType) {
        return R.layout.home_article_item;
    }

    @Override
    protected void onBindHolder(HomeArticleItemBinding binding, @NonNull BaseViewHolder<HomeArticleItemBinding> holder,
                                int position, @NonNull HomeArticleBean data, List<Object> payloads) {
        super.onBindHolder(binding, holder, position, data, payloads);

        binding.setHomeArticleBean(getData(position));
        binding.setHomeViewModel(homeViewModel);
        binding.executePendingBindings();

        binding.articleItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(v.getContext(), DetailsActivity.class);
                intent.putExtra("link", getData(position).getLink());
                v.getContext().startActivity(intent);
            }
        });

        binding.shineButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (binding.getHomeArticleBean().isCollect()) {
                    homeViewModel.cancelCollect(getData(position).getId());
                    binding.getHomeArticleBean().setCollect(false);
                    Log.e(TAG, "yes->no---------------------");
                } else {
                    homeViewModel.addCollect(getData(position).getId());
                    binding.getHomeArticleBean().setCollect(true);
                    Log.e(TAG, "no->yes---------------------");
                }
            }
        });
    }
}